package de.hauck.rtmidlet.views;

import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;

import de.hauck.rtmidlet.FileExImportInterface;
import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockStore;

public class FormSettingsMenu extends Form implements CommandListener,FileExImportInterface {
	Display _display;
	Displayable _dble;
	Configuration c;
	

	private final Command cOK = new Command("OK", Command.OK, 1);
	private final Command cCancel = new Command("Cancel", Command.BACK, 1);

	private Command cConfirmOK = new Command("OK", Command.OK, 1);
	
	private Command cResetYes = new Command("Yes", Command.OK, 1);
	private Command cResetNo = new Command("No", Command.BACK, 1);

	StockStore _myStore;
	RTMidlet _main;
	FormStockMaintainMenu _formMaintain;
	FormDebug _debugForm;
	FormLinks _linkForm;
	List settingsMenu, editStocksMenu;
	FormConfig _configForm;
	
	public FormSettingsMenu(Display display, Displayable dble,
			StockStore mystore, RTMidlet rtmidlet) {
		super("Setup");
		_myStore = mystore;
		_display = display;
		_main = rtmidlet;
		_dble = dble;
		String[] menuElements = { "Maintain stocks...", "Edit Links...", "Debug...",
				 "Reset DB", "Save DB","Config..." };
		settingsMenu = new List("Setup", Choice.IMPLICIT, menuElements, null);
		settingsMenu.addCommand(cOK);
		settingsMenu.addCommand(cCancel);
		settingsMenu.setSelectCommand(cOK);
		settingsMenu.setCommandListener(this);
		_main.show(settingsMenu);
	}

	public void commandAction(Command command, Displayable dble) {
		if (command == cCancel) {
			_main.showMenu();
		} else if (command == cOK) {
			switch (settingsMenu.getSelectedIndex()) {
			case 0: {
				// Edit stocks
				//if (_formMaintain == null)
					_formMaintain = new FormStockMaintainMenu(this, _myStore);
				_main.show(_formMaintain.getEditStocksMenu());
				break;
			}
			case 1: {
				// Edit Links
				//System.out.println("Edit Links");
				//if (_linkForm ==null)
				// always create new LinkForm, links could be added
				_linkForm=new FormLinks(_main, this);
				_main.show(_linkForm);
				break;
			}
			case 2: {
				// Debug
				//System.out.println("Debug");
				if (_debugForm ==null) _debugForm=new FormDebug(_main, this);
				_main.show(_debugForm);
				
				break;
			}
			
			case 3: {
				// Reset DB
				Alert reset_confirm = new Alert(
						"Reset DB",
						"Do you really want to reset the DB? Database will be deleted!",
						null, AlertType.CONFIRMATION);
				reset_confirm.addCommand(cResetYes);
				reset_confirm.addCommand(cResetNo);
				reset_confirm.setCommandListener(this);
				// import_confirm.setTimeout(5000);
				_main.show(reset_confirm);
				break;
			}
			case 4: {
				// Save DB
				try {
					_myStore.saveStocks();
					_myStore.saveConfig();
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			}case 5: {
				// Config always new (e.g. for import)		
				RTMidlet.getInstance().show(new FormConfig(this));
				break;
			}
			}

	
		} else if (command == cResetYes) {
			_myStore.resetDB(this);
			Configuration.dirtyFlagStocks=true;
			 _main.show(settingsMenu);
		} else if (command == cResetNo) {
			_main.show(settingsMenu);
		}
	}

	protected void keypressed(int key) {
		System.out.println(key);
	}

	public List getSettingsMenu() {
		return settingsMenu;
	}

	public void showResetConfirmation() {
		System.out.println("confirmation");
		Alert reset_info = new Alert("Information",
				"Database was resetted succesfully", null, AlertType.INFO);
		reset_info.addCommand(cConfirmOK);
		reset_info.setCommandListener(this);
		_main.show(reset_info);
	}

	public void showExportSuccess() {
		// TODO Auto-generated method stub
		
	}

	public void showImportSuccess() {
		// TODO Auto-generated method stub
		
	}
	
	
}
